package com.example.heimaai.tools;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.example.heimaai.entity.po.Course;
import com.example.heimaai.entity.po.CourseReservation;
import com.example.heimaai.entity.po.School;
import com.example.heimaai.entity.query.CourseQuery;
import com.example.heimaai.service.ICourseReservationService;
import com.example.heimaai.service.ICourseService;
import com.example.heimaai.service.ISchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class CourseTool {
    private final ICourseService courseService;
    private final ISchoolService  schoolService;
    private final ICourseReservationService courseReservationService;

    //表明不是普通的方法
    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(description = "查询的条件")CourseQuery query){
        if (query==null){
            return courseService.list();
        }
        QueryChainWrapper<Course> wrapper = courseService.query()
                .eq(query.getType() != null, "type", query.getType())
                .le(query.getEdu() != null, "edu", query.getEdu());
        //不能直接排序，要判断是否非空
        if (query.getSorts()!=null&&!query.getSorts().isEmpty()){
            List<CourseQuery.Sort> sorts = query.getSorts();
            //可能有多个排序字段
            for (CourseQuery.Sort sort : sorts) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }

        }
        return wrapper.list();
    }
    @Tool(description = "查询所有校区")
    public List<School> querySchool(){
        return schoolService.list();
    }
    @Tool(description = "生成预约单,返回预约单号")
    public Integer createCourseReservation(@ToolParam(description = "预约课程")String course,
                                           @ToolParam(description = "预约校区")String school,
                                           @ToolParam(description = "学生姓名")String studentName,
                                           @ToolParam(description = "联系电话")String contactInfo,
                                           @ToolParam(description = "备注",required = false)String remark){
        CourseReservation courseReservation = CourseReservation.builder()
                .course(course)
                .school(school)
                .studentName(studentName)
                .contactInfo(contactInfo)
                .remark(remark)
                .build();
        courseReservationService.save(courseReservation);
        return courseReservation.getId();
    }
}
